// component/uploadCom/uploadCom.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    fileLength: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose: function () {
      this.triggerEvent("onClose");
    },
    uploadImage: async function () {
      const that = this;
      wx.chooseMedia({
        count: 9,  //最多可以选择的图片总数
        sizeType: ['compressed', 'original'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        mediaType: ['image'],
        success(res) {
          that.onClose();
          const tempFilePaths = res.tempFiles;
          if (that.properties.fileLength + tempFilePaths.length > 9) {
            wx.showModal({
              title: '提示',
              content: '最多上传9个',
              showCancel: false,
              success: function (res) { }
            })
            return;
          }
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 1000
          })

          tempFilePaths.forEach(item => {
            wx.compressImage({
              src: item.tempFilePath, // 图片路径
              quality: 50, // 压缩质量，0-100，数值越小压缩率越高
              success: (compressedRes) => {
                // 使用压缩后的图片路径进行上传等操作
                that.uploadHttp(compressedRes.tempFilePath);
              },
              fail: (error) => {
                that.uploadHttp(item.tempFilePath);
              }
            });
          })
        }
      })
    },

    uploadHttp(url) {
      const that = this;
      wx.uploadFile({
        url: `${app.globalData.baseUrl}/common/upload`,
        filePath: url,
        name: 'file',
        formData: {
          'user': 'test'
        },
        success: function (res) {
          const datas = JSON.parse(res.data);
          that.triggerEvent("sendData", datas.url);
          wx.showToast({
            title: '图片上传成功',
            icon: 'loading',
            showCancel: false,
            duration: 1000
          })
        },

        fail: function (res) {
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function (res) { }
          })
        }
      });
    },

    chooseFile: async function () {
      const that = this;
      wx.chooseMessageFile({
        count: 9,  //最多可以选择的图片总数
        type: 'image',
        success(res) {
          that.onClose();
          const tempFilePaths = res.tempFiles;
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 1000
          })

          tempFilePaths.forEach(item => {
            wx.compressImage({
              src: item.path, // 图片路径
              quality: 50, // 压缩质量，0-100，数值越小压缩率越高
              success: (compressedRes) => {
                // 使用压缩后的图片路径进行上传等操作
                that.uploadHttp(compressedRes.tempFilePath);
              },
              fail: (error) => {
                that.uploadHttp(item.tempFilePath);
              }
            });
          })
        }
      })
    }
  }
})
